package com.icode.service.impl.house;

import com.icode.core.dto.house.ExchangePoolDTO;
import com.icode.core.model.house.*;
import com.icode.core.model.user.User;
import com.icode.core.model.user.UserInfo;
import com.icode.dao.ExchangeDao;
import com.icode.dao.RentDao;
import com.icode.service.house.ExchangeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: xianfei
 * Date: 15-6-8
 * Time: 下午3:28
 * To change this template use File | Settings | File Templates.
 */
@Service("exchangeService")
@Transactional
public class ExchangeServiceImpl implements ExchangeService {

    @Autowired
    private ExchangeDao exchangeDao;

    @Autowired
    private RentDao rentDao;

    @Override
    @Transactional
    public boolean addToExchangePool(String myHouseGuid) {
        boolean result = true;
        try {
            MyHouse myHouse = exchangeDao.findByGuid(MyHouse.class, myHouseGuid);
            Rent rent = rentDao.findRentByMyHouse(myHouse);
            if (null == rent) {
                if (myHouse.myHouseStatus() == null || MyHouseStatus.ACTIVITY.equals(myHouse.myHouseStatus())) {
                    ExchangePool exchangePool = new ExchangePool(myHouse);
                    myHouse.setMyHouseStatus(MyHouseStatus.CHANGING);
                    exchangeDao.saveOrUpdate(exchangePool);
                    myHouse.userInfo().setExchangeNum((myHouse.userInfo().exchangeNum() + 1));
                } else {
                    result = false;
                }
            }
        } catch (Exception e) {
            result = false;
        }
        return result;
    }

//    @Override
//    @Transactional
//    public boolean addToExchangePool(String myHouseGuid) {
//        boolean result = true;
//        try {
//            MyHouse myHouse = exchangeDao.findByGuid(MyHouse.class, myHouseGuid);
//            if (myHouse.days()<7 && MyHouseStatus.ACTIVITY.equals(myHouse.myHouseStatus())) {
//                ExchangePool exchangePool = new ExchangePool(myHouse);
//               // myHouse.setMyHouseStatus(MyHouseStatus.CHANGING);
//                exchangeDao.saveOrUpdate(exchangePool);
//                myHouse.updateDays(1);
//                myHouse.userInfo().setExchangeNum((myHouse.userInfo().exchangeNum() + 1));
//            } else {
//                result = false;
//            }
//        } catch (Exception e) {
//            result = false;
//        }
//        return result;
//    }

    @Override
    @Transactional
    public List<ExchangePoolDTO> findAllExchangePool(String type, String userInfoGuid) {
        List<ExchangePool> exchangePools = new ArrayList<>();

        if ("HOUSEBASE".equals(type)) {
            exchangePools = exchangeDao.findExchangePoolByHouseBase(userInfoGuid);
        } else if ("APARTMENT".equals(type)) {
            exchangePools = exchangeDao.findExchangePoolByApartment(userInfoGuid);
        } else {
            exchangePools = exchangeDao.findAllExchangePools(userInfoGuid);
        }
        List<ExchangePoolDTO> exchangePoolDTOs = ExchangePoolDTO.toDTOs(exchangePools);
        return exchangePoolDTOs;
    }

    @Override
    @Transactional
    public boolean doExchange(String userExchangePoolGuid, String exchangePoolGuid, User user) {
        boolean result = true;
        try {
            ExchangePool exchangePool = exchangeDao.findByGuid(ExchangePool.class, exchangePoolGuid);
            UserInfo userInfo = exchangeDao.findUserInfoByUser(user);
            if (userInfo == null || userInfo.exchangeNum() <= 0) {
                throw new Exception("exchangeNum is not enough");
            }
            ExchangeHouseInfo exchangeHouseInfo = new ExchangeHouseInfo(exchangePool.myHouse().houseInfo(), exchangePool.myHouse().houseWeek(), userInfo);
            exchangeDao.saveOrUpdate(exchangeHouseInfo);
            userInfo.setExchangeNum(userInfo.exchangeNum() - 1);
            ExchangePool userExchangePool = exchangeDao.findByGuid(ExchangePool.class, userExchangePoolGuid);
            userExchangePool.myHouse().setUsable(false);
            MyHouse myHouse = new MyHouse(exchangePool.myHouse().houseInfo(), exchangePool.myHouse().houseWeek(), userInfo, MyHouseStatus.CHANGEHOUSE);
            myHouse.setExchangeHouse(true);
            exchangeDao.saveOrUpdate(myHouse);
        } catch (Exception e) {
            result = false;
        }
        return result;
    }

    @Override
    @Transactional
    public List<ExchangePoolDTO> findExchangePoolByUserInfo(String myHouseGuid) {
        MyHouse myHouse = exchangeDao.findByGuid(MyHouse.class, myHouseGuid);
        List<ExchangePool> exchangePools = exchangeDao.findExchangePoolByUserInfo(myHouse.userInfo());
        List<ExchangePoolDTO> exchangePoolDTOs = ExchangePoolDTO.toDTOs(exchangePools);
        return exchangePoolDTOs;
    }

    @Override
    @Transactional
    public ExchangePoolDTO findExchangePoolByGuid(String exchangePoolGuid) {
        ExchangePool exchangePool = exchangeDao.findByGuid(ExchangePool.class, exchangePoolGuid);
        return ExchangePoolDTO.toDTO(exchangePool);
    }

    @Override
    @Transactional
    public ExchangePool findSingleExchangePool(String exchangePoolGuid) {
        ExchangePool exchangePool = exchangeDao.findByGuid(ExchangePool.class, exchangePoolGuid);
        return exchangePool;
    }

    @Override
    @Transactional
    public BigDecimal toConfirmExchange(String userExchangePoolGuid, String exchangePoolGuid) {
        ExchangePool userExchangePool = exchangeDao.findByGuid(ExchangePool.class, userExchangePoolGuid);
        ExchangePool exchangePool = exchangeDao.findByGuid(ExchangePool.class, exchangePoolGuid);
        BigDecimal myHousePrice = new BigDecimal(userExchangePool.myHouse().houseWeek().price());
        BigDecimal exchangePrice = new BigDecimal(exchangePool.myHouse().houseWeek().price());
        BigDecimal subtract = exchangePrice.subtract(myHousePrice);
        return subtract;
    }

    @Override
    @Transactional
    public void chooseExchange(String myHouseGuid) {
        MyHouse myHouse = exchangeDao.findByGuid(MyHouse.class, myHouseGuid);
        if (myHouse.myHouseStatus() == null || !MyHouseStatus.CHANGING.equals(myHouse.myHouseStatus())) {
            ExchangePool exchangePool = new ExchangePool(myHouse);
            exchangeDao.saveOrUpdate(exchangePool);
            myHouse.setMyHouseStatus(MyHouseStatus.CHANGING);
            myHouse.userInfo().setExchangeNum((myHouse.userInfo().exchangeNum() + 1));
        }
    }

    @Override
    @Transactional
    public List<ExchangePoolDTO> findExchangePoolByMyHouse(String myHouseGuid) {
        MyHouse myHouse = exchangeDao.findByGuid(MyHouse.class, myHouseGuid);
        List<ExchangePool> exchangePools = exchangeDao.findExchangePoolNotUserInfo(myHouse.userInfo());
        List<ExchangePoolDTO> exchangePoolDTOs = ExchangePoolDTO.toDTOs(exchangePools);
        return exchangePoolDTOs;
    }

    @Override
    @Transactional
    public String getServiceFee(String exchangePoolGuid) {
        String fee = "100";
        ExchangePool exchangePool = exchangeDao.findByGuid(ExchangePool.class, exchangePoolGuid);
        if (ProductType.HOUSEBASE.equals(exchangePool.myHouse().houseInfo().buildingType().houseBase().productType())) {
            fee = "200";
        }
        return fee;
    }

    @Override
    public List<ExchangePoolDTO> findExchangeHouseByUsername(String username) {
        List<ExchangePool> exchangePools = exchangeDao.findExchageHouseByUsername(username);
        List<ExchangePoolDTO> exchangePoolDTOs = new ArrayList<>();
        if (exchangePoolDTOs != null){
            exchangePoolDTOs = ExchangePoolDTO.toDTOs(exchangePools);
        }
        return exchangePoolDTOs;
    }


}
